package com.oocl.admin_platform.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.admin_platform.dto.MerchantDTO;
import com.oocl.admin_platform.pojo.Merchant;


public class JsonUtil {
	
	public static ObjectMapper mapper = new ObjectMapper();
	
	public static MerchantDTO toDTO(String json) throws JsonParseException, JsonMappingException, IOException{
		MerchantDTO merchantDTO = mapper.readValue(json, MerchantDTO.class);
		return merchantDTO;
	}
	
	public static String toJson(MerchantDTO merchantDTO) throws JsonProcessingException{
		String json = mapper.writeValueAsString(merchantDTO);
		return json;
	}
//	
	public static String listToJson(List<MerchantDTO> list) throws JsonProcessingException{
		String json = mapper.writeValueAsString(list);
		return json;
	}
	
	
	public static List<MerchantDTO> toDTOList(String json) throws JsonParseException, JsonMappingException, IOException{
		List<MerchantDTO> merchantDTOList = mapper.readValue(json, new TypeReference<List<MerchantDTO>>() {});
		return merchantDTOList;
	}
	
	public static String stringToString(String s) throws JsonProcessingException {
		String jString = mapper.writeValueAsString(s);
		 return jString;
	}
	

	
//	public static void main(String[] args) throws IOException {
//		MerchantDTO merchantDTO = new MerchantDTO("qwer", "hello".getBytes(), "portrait".getBytes(), "address", 2, "eclipse");
//		MerchantDTO merchantDTO2 = new MerchantDTO("asdf", "aaaa".getBytes(), "bbbb".getBytes(), "address", 2, "eclipse");
//		List<MerchantDTO> list = new ArrayList<MerchantDTO>();
//		list.add(merchantDTO);
//		list.add(merchantDTO2);
//		System.out.println(toJson(list));
//		List<MerchantDTO> receive = toDTO(toJson(list));
//		for (MerchantDTO merchantDTO3 : receive) {
//			System.out.println(merchantDTO3);
//		}
////		System.out.println(merchantDTO.toString());
////		System.out.println(toJson(merchantDTO));
////		System.out.println(toDTO(toJson(merchantDTO)));
//	}
}
